package com.corecloud.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.corecloud.entity.SysPermissionRoleEntity;
import com.corecloud.entity.SysRoleEntity;
import com.corecloud.entity.SysUserRoleEntity;
import com.corecloud.service.SysPermissionRoleService;
import com.corecloud.service.SysRoleService;
import com.corecloud.service.SysUserRoleService;
import com.corecloud.tools.AjaxResultData;
import com.corecloud.tools.PageDemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.security.Principal;

/**
 * 角色相关操作
 */
@Controller
@RequestMapping("/role")
public class RoleController {

	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	private SysPermissionRoleService sysPermissionRoleService;
	@Autowired
	private SysUserRoleService sysUserRoleService;

	/**
	 * 角色列表页
	 * @param principal 登录用户信息
	 * @return
	 */
	@RequestMapping("/index")
	public ModelAndView index(Principal principal){
		ModelAndView modelAndView = new ModelAndView("role/index");
		return modelAndView;
	}

	/**
	 * 添加角色
	 * @param principal
	 * @return
	 */
	@RequestMapping("/add")
	public ModelAndView add(Principal principal){
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("role/add");
		return modelAndView;
	}

	/**
	 * 修改角色
	 * @param principal 登录用户信息
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit(Principal principal,Integer id){
		ModelAndView modelAndView = new ModelAndView("role/edit");
		//查询信息,回填页面
		if(!StringUtils.isEmpty(id)){
			SysRoleEntity paras = sysRoleService.getByParas(new QueryWrapper<SysRoleEntity>().eq("id", id));
			modelAndView.addObject("roleInfo", paras);
		}
		return modelAndView;
	}

	/**
	 * 删除角色
	 * @param principal 登录用户信息
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public AjaxResultData delete(Principal principal,Integer id){
		if(StringUtils.isEmpty(id)){
			return new AjaxResultData(500,"参数异常,请联系管理员");
		}
		try {
			sysRoleService.deleteInfo(id);
			sysPermissionRoleService.deleteInfo(new QueryWrapper<SysPermissionRoleEntity>().eq("rid",id));
			sysUserRoleService.deleteByParas(new QueryWrapper<SysUserRoleEntity>().eq("role_id",id));
			return new AjaxResultData("操作成功");
		}catch (Exception e){
			return new AjaxResultData(500,"操作失败,请联系管理员");
		}
	}

	/**
	 * 保存添加或修改信息
	 * @param principal 登录用户信息
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public AjaxResultData delete(Principal principal, String tag, SysRoleEntity info){
		String name = info.getName().trim();
		String code = info.getCode().trim();
		if(StringUtils.isEmpty(tag)){ return new AjaxResultData(500,"参数异常,请联系管理员"); }
		if(StringUtils.isEmpty(name)){ return new AjaxResultData(500,"请填写角色名称"); }
		if(StringUtils.isEmpty(code)){ return new AjaxResultData(500,"请填写角色代码"); }

		Integer id = 0;

		try {
			if(tag.equals("add")){
				SysRoleEntity sr = sysRoleService.getByParas(new QueryWrapper<SysRoleEntity>().eq("name", name));
				if(sr != null && !StringUtils.isEmpty(sr.getName())){ return new AjaxResultData(500,"角色名称被占用"); }

				sr = sysRoleService.getByParas(new QueryWrapper<SysRoleEntity>().eq("code", code));
				if(sr != null && !StringUtils.isEmpty(sr.getCode())){ return new AjaxResultData(500,"角色代码被占用"); }

				sysRoleService.insertInfo(info);

				QueryWrapper<SysRoleEntity> sysRoleEntity2 = new QueryWrapper<SysRoleEntity>();
				sysRoleEntity2.eq("name", name);
				sysRoleEntity2.eq("code", code);
				SysRoleEntity byParas = sysRoleService.getByParas(sysRoleEntity2);
				id = byParas.getId();
			}else{
				if(StringUtils.isEmpty(info.getId())){ return new AjaxResultData(500,"参数异常,请联系管理员"); }

				SysRoleEntity sr = sysRoleService.getByParas(new QueryWrapper<SysRoleEntity>().eq("name", name));
				if(sr != null && !StringUtils.isEmpty(sr.getName()) && !sr.getId().equals(info.getId())){
					return new AjaxResultData(500,"角色名称被占用");
				}

				sr = sysRoleService.getByParas(new QueryWrapper<SysRoleEntity>().eq("code", code));
				if(sr != null && !StringUtils.isEmpty(sr.getCode())&& !sr.getId().equals(info.getId())){
					return new AjaxResultData(500,"角色代码被占用");
				}

				//更新角色表信息
				sysRoleService.updateInfo(info);
				//更新角色权限关联表信息
				sysPermissionRoleService.updateInfoByRid(info.getId());
				//更新角色用户关联表信息
				sysUserRoleService.updateInfoByRid(info.getId());

				id = 200;
			}
			return new AjaxResultData(id, "操作成功");
		}catch (Exception e){
			return new AjaxResultData(500,"操作失败,请联系管理员");
		}

	}

	/**
	 * 角色列表页数据（分页）
	 * @param principal 登录用户信息
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public Page<SysRoleEntity> list(Principal principal, SysRoleEntity sr, PageDemo pageDemo){
		QueryWrapper<SysRoleEntity> qw = new QueryWrapper();
		//拼装查询条件
		if(!StringUtils.isEmpty(sr.getIsValid())){
			qw.eq("is_valid",sr.getIsValid());
		}
		if(!StringUtils.isEmpty(sr.getName())){
			qw.like("name",sr.getName());
		}
		OrderItem oi = new OrderItem();
		oi.setAsc(false);
		oi.setColumn("id");
		pageDemo.addOrder(oi);
		return sysRoleService.getInfoByPage(pageDemo,qw);
	}

}
